﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RadioThermostat.Common;
using Newtonsoft.Json;

namespace RadioThermostat
{
    public class RTTempSettings
    {
        private readonly IJSONDecoder JSON = JSONNetDecoderv.Instance;
        private readonly RTThermostat thermostat;
        private readonly WebCommunicator webCommunicator;

        internal RTTempSettings(RTThermostat thermostat)
        {
            this.thermostat         = thermostat;
            this.webCommunicator    = thermostat.WebCommunicator;
        }

        public RTTstat GetTstat()
        {
            string responce = webCommunicator.HttpGet(RTUtils.GetURL(this.thermostat, "tstat"));
            return JSON.Deserialize<RTTstat>(responce);            
        }

        public RTttemp GetTempPoints()
        {
            string url = RTUtils.GetURL(this.thermostat, "tstat/ttemp");
            string response = webCommunicator.HttpGet(url);
            return JSON.Deserialize<RTttemp>(response);
        }

        public RTTstatDatalog GetTstatDataLog()
        {
            string url = RTUtils.GetURL(this.thermostat, "tstat/datalog");
            string responce = webCommunicator.HttpGet(url);
          
            return JSON.Deserialize<RTTstatDatalog>(responce);
        }

        public double GetCurrentTemp()
        {
            string url = RTUtils.GetURL(this.thermostat, "tstat/temp");
            string responce = webCommunicator.HttpGet(url);
            return RTUtils.GetSingleDictionaryVal<double>(JSON, responce); 
        }

        public double GetHumidity()
        {
            string url = RTUtils.GetURL(this.thermostat, "tstat/humidity");
            string responce = webCommunicator.HttpGet(url);
            return RTUtils.GetSingleDictionaryVal<double>(JSON, responce); 
        }

        public TempMode GetTstatMode()
        {
            string url = RTUtils.GetURL(this.thermostat, "tstat/tmode");
            string jason = webCommunicator.HttpGet(url);
            return (TempMode)RTUtils.GetSingleDictionaryVal<int>(JSON, jason);
        }

        public TState GetTState()
        {
            string url = RTUtils.GetURL(this.thermostat, "tstat/tstate");
            string jason = webCommunicator.HttpGet(url);
            return (TState)RTUtils.GetSingleDictionaryVal<int>(JSON, jason);
        }

        public string GetProgram()
        {
            string url = RTUtils.GetURL(thermostat, "tstat/program");
            string jason = webCommunicator.HttpGet(url);
            return "";
        }

        public FanMode GetFanMode()
        {
            string url = RTUtils.GetURL(this.thermostat, "tstat/fmode");
            string jason = webCommunicator.HttpGet(url);
            FanMode fm = new FanMode();
            fm = (FanMode)RTUtils.GetSingleDictionaryVal<int>(JSON, jason);
            return fm;
        }

        public void SetFanMode(FanMode fanMode)
        {
            string json = JSON.GetJson(new Dictionary<string, int> { { "fmode", (int)fanMode } });
            string url = RTUtils.GetURL(thermostat,"tstat");
        }

        public void SetTHeat(double temp)
        {
            string json = JSON.GetJson(new Dictionary<string, double> { { "t_heat", temp } });                                                                       
            string url = RTUtils.GetURL(thermostat, "tstat");
            webCommunicator.HttpPost(url, json);
        }

        public void SetTMode(TempMode mode)
        {
            string json = JSON.GetJson(new Dictionary<string, int> { { "tmode", ((int)mode) } });        
            string url = RTUtils.GetURL(thermostat, "tstat");
            webCommunicator.HttpPost(url, json);
        }

        public void GetProgram2()
        {
           


        }

    }

    public enum TempMode{OFF=0,HEAT=1,COOL=2,AUTO=3};
    public enum FanMode { AUTO = 0, OFF = 1, ON = 2};
    public enum TState { OFF = 0, HEAT = 1, COOL = 2 };
    public enum FState { OFF = 0, ON = 1 };





    class RTProgramWrap
    {
        [JsonProperty(PropertyName = "0")]
        public List<int> Sunday { get; set; }

        [JsonProperty(PropertyName = "1")]
        public List<int> Monday { get; set; }

        [JsonProperty(PropertyName = "2")]
        public List<int> Tuesday { get; set; }

        [JsonProperty(PropertyName = "3")]
        public List<int> Wednesday { get; set; }

        [JsonProperty(PropertyName = "4")]
        public List<int> Thursday { get; set; }

        [JsonProperty(PropertyName = "5")]
        public List<int> Friday { get; set; }

        [JsonProperty(PropertyName = "6")]
        public List<int> Saturday { get; set; }
    }
    
}
